function GetTimeSpan(timeStart) {
    tDate = timeStart.slice(0, 10);
    tTime = timeStart.slice(11, 19);
    timeStart = tDate + " " + tTime;
    var date1 = new Date(timeStart.replace(/-/g, '/'));

    // See if the original date falls between the start and end of DST for the specified year so
    // we know how much to adjust the date by when converting it to UTC.
    isDST = isDSTDate(date1);

    // We assume that the original date is PST so we always need to adjust by at least +7 hours when converting to UTC.
    var oneHour = 1000 * 60 * 60;
    date1.setTime(date1.getTime() + oneHour * 7);

    // If the original date did not occur when DST was in effect then we need to add an additional hour when converting to UTC.
    if (!isDST) {
        date1.setTime(date1.getTime() + oneHour);
    }

    // Convert the UTC datetime to the browser's local datetime by adjusting by the TZ offset.
    var tzOffset = date1.getTimezoneOffset();
    var date2 = new Date(date1.valueOf() - tzOffset * 60 * 1000);
    return date2.toRelativeString();
}
function isDSTDate(date) {
    // The following code assumes that the supplied date is for a TZ that uses DST.
    // This is not a valid assumption for all TZ but is OK if we assume that we're
    // only concerned with dates that are relative to Pacific Standard Time (PST)
    // where DST is used.

    var myDate = new Date(date);
    var isDLS = false;
    var gmt = new Date(date); ; //new Date;
    var ssm = new Date(date); ;
    var fsn = new Date(date); ;
    var aDay = 1000 * 60 * 60 * 24;

    // Find the start date of DST for the specified year.
    // DST starts on the second Sunday in March.

    ssm.setMonth(2);
    ssm.setDate(1);
    var counter = 0;
    for (i = 0; i < 15; i++) {
        if (counter == 2) {
            break;
        }
        if (ssm.getDay() == 6) {
            counter++;
        }
        ssm.setTime(ssm.getTime() + aDay);
    }

    // Find the end date of DST for the specified year.
    // DST ends on the first Sunday in November.

    fsn.setMonth(10);
    fsn.setDate(1);

    for (i = 0; i < 7; i++) {
        if (fsn.getDay() == 6) {
            fsn.setTime(fsn.getTime() + aDay);
            break;
        }
        else {
            fsn.setTime(fsn.getTime() + aDay);
        }
    }

    // Return true if the specified date falls between the start and end of DST for the specified year.

    if (gmt < ssm || gmt >= fsn) {
        isDLS = false;
    }
    else {
        isDLS = true;
    }

    return isDLS;
}

function ShowMessagePage(otmessageid) {
    var options = {
        title: 'Conversation',
        allowMaximize: false,
        showClose: true,
        url: "../SitePages/OfficeTalkThread.aspx?messageId=" + otmessageid,
        width: 800,
        height: 600//,
        // dialogReturnValueCallback: DialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
    event.cancelBubble = true;
}
function ShowThreadPage(alias) {
    var options = {
        title: 'Posts',
        allowMaximize: false,
        showClose: true,
        url: "../SitePages/OfficeTalkThread.aspx?posts=" + alias,
        width: 800,
        height: 600//,
        // dialogReturnValueCallback: DialogCallback
    };
    SP.UI.ModalDialog.showModalDialog(options);
    event.cancelBubble = true;
}
function TruancateMsg(msg) {
    var maxChrs = 90;
    var ellipsis = ' ...';
    var maxlen = 0;
    var firstlen = 0;
    var secondlen = 0;
    var firstlen = msg.lastIndexOf(" ", maxChrs);
    if (firstlen < 1) {
        firstlen = msg.lastIndexOf("-", maxChrs);
        if (firstlen < 1) {
            firstlen = maxChrs;
        }
    }
    maxChrs += firstlen;
    if (msg.length > maxChrs) {
        secondlen = msg.lastIndexOf(" ", maxChrs);
        if (secondlen > firstlen) {
            maxlen = secondlen;
        }
        else {
            maxlen = maxChrs;
        }
        msg = msg.substring(0, maxlen) + ellipsis;
    }
    return msg;
}

function HitHighlightedMessage(id) {
    // var addhtml = "<div style=\"position: absolute; right: 0; bottom: 0; z-index: 10; background-color: #FFFFFF;\"> ...</div>";
    var msg = document.getElementById("msg-" + id).innerHTML;
    msg = msg.TruancateMsg(msg);
    var keys = document.getElementById("hl-" + id).innerHTML;
    pattern = /(?!(<c\d>))([^<>]+?)(?=<\/c\1)/gi;
    var keyObj = new Object();
    var keyArr = new Array();
    var kArr = keys.match(pattern);
    var keyStr = "";
    if (kArr != null) {
        for (i = 0; i < kArr.length; i++) {
            k = kArr[i].replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/gi, "\\$1");
            if (keyObj[k] == null) {
                keyArr.push("\\b" + k + "\\b");
                keyObj[k] = k;
            }
        }
        if (keyArr.length > 0) {
            keyStr = keyArr.join("|");
            repReg = new RegExp(keyStr, "gi");
            res = msg.replace(repReg, function ($0, $1, $2) {
                return "<strong>" + $0 + "</strong>";
            });
            document.getElementById("msg-" + id).innerHTML = res;
        }
    }
    else {
        document.getElementById("msg-" + id).innerHTML = msg;
    }
}
